-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added monte carlo javascript example #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added monte carlo javascript example #175
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! You're really putting in some work. Thanks for another request!
First off, we use 2 space indentation for JavaScript in the Algorithm Archive.
We have a .editorconfig file in the repo, so if you want, you can install the EditorConfig plugin for your editor of choice and never have to worry about indentation inside the AAA again. You don't have to, though.
@@ -0,0 +1,26 @@ | |||
// submitted by xam4lor | |||
function inCircle(xPos, yPos, radius) { | |||
if(xPos * xPos + yPos * yPos < radius * radius) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just do this:
function inCircle(xPos, yPos, radius) {
return xPos * xPos + yPos * yPos < radius * radius;
}
} | ||
|
||
let piEstimate = 4 * Math.PI / (n * (radius * radius)); | ||
console.log('Percent error is: %s%', piEstimate); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's one "%" too much here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because the %s is to insert the string et the last % is because the value is in % !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it shows, for example : "Percent error is: 0.2%"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right. My bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly const/let/var
nitpicks, other than that the implementation looks good. For the formatting I recommend installing prettier
for your editor, it'll make your life much easier.
function monteCarlo(n, radius) { | ||
let piCount = 0; | ||
|
||
for (var i = 1; i < n; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That var
should be a let
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it should probably start from 0
if you want n
iterations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did I not catch this? Maybe I should think about going to bed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did I not too 🤣
let pointX = Math.random(); | ||
let pointY = Math.random(); | ||
|
||
if(inCircle(pointX, pointY, radius)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS also has spaces around parens: if (...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. At this point I'm just handing this over to you. You seem to be in a much clearer state of mind than I am right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was a mistake !
let piCount = 0; | ||
|
||
for (var i = 1; i < n; i++) { | ||
let pointX = Math.random(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pointX
and pointY
should be const
instead of let
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about commenting this, but I think it's more of a preference thing, isn't it? I don't think we ever decided to enforce this style in the AAA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's recommended by pretty much every JS/TS linter on the default settings. I think it's a reasonable request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The discussion about using const vs let is almost just for readability but It's a reasonable request, as you said.
} | ||
} | ||
|
||
let piEstimate = 4 * Math.PI / (n * (radius * radius)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also be const
I really like this project because It has so much potential! And I'm almost in holidays, so ... :) |
@xam4lor Thanks for all the help so far! We have a lot left to do, but definitely growing! |
You can thanks 3blue1brown and this video for speaking about you too! |
@xam4lor yeah, that shoutout really boosted the number of people contributing to this project. I'm pretty excited to see where we go from here! (sorry for the off-topic conversation) |
It seems like all we're changing all the Monte Carlo implementations to get rid of the "radius" variable. Could you take a look at the Julia one and make similar changes in the JS implementations as well? |
Me too! |
@Butt4cak3 could you approve and merge this one? Your change request is blocking the merge from the web UI and I don't have a desktop now. |
@Gustorn Oops. Thought I already approved. |
No description provided.